package com.xy.mainp.novel.fragment.home.ui;

import android.support.annotation.NonNull;

import com.xy.mainp.model.biqu.home.BiquHomeHotEntity;
import com.xy.mainp.model.biqu.home.classify.ClassfiyBookEntity;
import com.xy.mainp.model.biqu.home.classify.ClassfiyBookItemEntity;
import com.xy.mainp.model.biqu.home.newbook.LastInBookEntity;
import com.xy.mainp.model.biqu.home.newbook.LastInBookItemEntity;
import com.xy.mainp.model.biqu.home.newbook.LastUpdateEntity;
import com.xy.mainp.model.biqu.home.newbook.LastUpdateItemEntity;
import com.xy.mainp.model.biqu.home.novellist.NovelItemListEntity;
import com.xy.mainp.model.biqu.home.novellist.NovelItemTopEntity;
import com.xy.mainp.model.biqu.home.novellist.NovelListEntity;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by John on 2016/12/5.
 */

public class NovelHomeModel implements NovelHomeCanstract.NovelHomeModelImpl {

    @Override
    public LastInBookEntity getLastIn(Document document) {
        Element lastIn = document.select("div[id=newscontent] div.r").first();
        LastInBookEntity InEntity = new LastInBookEntity();
        InEntity.setTittle(lastIn.select("h2").text());
        Elements elements = lastIn.select("ul li");
        ArrayList<LastInBookItemEntity> itemEntities = new ArrayList<>();
        for (int i = 0; i < elements.size(); i++) {
            Element element = elements.get(i);
            LastInBookItemEntity entity = new LastInBookItemEntity();
            entity.setType(element.select("span.s1").text());
            entity.setBookName(element.select("span.s2 a").text());
            entity.setBookUrl(element.select("span.s2 a").attr("href"));
            entity.setBookAuthor(element.select("span.s5").text());
            itemEntities.add(entity);
        }
        InEntity.setItems(itemEntities);
        return InEntity;
    }

    @Override
    public LastUpdateEntity getLastUpdate(Document document) {
        Element lastUpdate = document.select("div[id=newscontent] div.l").first();
        LastUpdateEntity updateEntity = new LastUpdateEntity();
        updateEntity.setTittle(lastUpdate.select("h2").text());
        Elements elements = lastUpdate.select("ul li");
        ArrayList<LastUpdateItemEntity> itemEntities = new ArrayList<>();
        for (int i = 0; i < elements.size(); i++) {
            Element element = elements.get(i);
            LastUpdateItemEntity entity = new LastUpdateItemEntity();
            entity.setBookType(element.select("span.s1").text());
            entity.setBookName(element.select("span.s2 a").text());
            entity.setBookUrl(element.select("span.s2 a").attr("href"));
            entity.setBookChapter(element.select("span.s3 a").text());
            entity.setBookChapterUrl(element.select("span.s3 a").attr("href"));
            entity.setBookAuthor(element.select("span.s4").text());
            entity.setBookUpdateTime(element.select("span.s5").text());
            itemEntities.add(entity);
        }
        updateEntity.setItems(itemEntities);

        return updateEntity;

    }

    @Override
    public ClassfiyBookEntity getClassifiyBooks(Document document) {
        List<ClassfiyBookItemEntity> classfiyBooklists = new ArrayList<>(); //经典推荐
        Elements ClassfiyBooks = document.select("div[id=hotcontent]").select("div.r");
        String ClassfiyTittle = ClassfiyBooks.select("h2").first().text();
        Elements classfies = ClassfiyBooks.select("ul li");
        ClassfiyBookEntity bookEntity = new ClassfiyBookEntity();
        bookEntity.setTittle(ClassfiyTittle);
        for (int i = 0; i < classfies.size(); i++) {
            ClassfiyBookItemEntity entity = new ClassfiyBookItemEntity();
            Element classfiy = classfies.get(i);
            entity.setType(classfiy.select("span.s1").text());
            entity.setBookUrl(classfiy.select("span.s2 a").attr("href"));
            entity.setBookName(classfiy.select("span.s2 a").text());
            entity.setBookAuthor(classfiy.select("span.s5").text());
            classfiyBooklists.add(entity);
        }
        bookEntity.setItemEntities(classfiyBooklists);
        return bookEntity;
    }

    @Override
    public List<BiquHomeHotEntity> getHotBooks(Document document) {
        List<BiquHomeHotEntity> hotEntities = new ArrayList<>();  //热门书籍
        Elements hotBooks = document.select("div[id=hotcontent]").select("div.l div.item");
        for (int i = 0; i < hotBooks.size(); i++) {
            BiquHomeHotEntity entity = new BiquHomeHotEntity();
            Element hotbook = hotBooks.get(i);
            entity.setBookImgUrl(hotbook.select("div.image a img").attr("src"));
            entity.setBookAuthor(hotbook.select("dl dt span").text());
            entity.setBookName(hotbook.select("dl dt a").text());
            entity.setBookReadUrl(hotbook.select("div.image a").attr("href"));
            entity.setBookDesc(hotbook.select("dl dd").text());
            hotEntities.add(entity);
        }
        return hotEntities;
    }

    @Override
    @NonNull
    public NovelListEntity getNovelListEntity(Element s) {
        NovelListEntity list = new NovelListEntity();

        String NovelListh2 = s.select("h2").text();
        list.setNovelListTittle(NovelListh2);

        Element topBook = s.select("div.top").first();
        NovelItemTopEntity topEntity = new NovelItemTopEntity();
        topEntity.setImgUrl(topBook.select("div.image img").attr("src"));
        topEntity.setBookDesc(topBook.select("dl dd").text());
        topEntity.setBookUrl(topBook.select("dl dt a").attr("href"));
        topEntity.setBookName(topBook.select("dl dt a").text());
        list.setTopEntity(topEntity);

        ArrayList<NovelItemListEntity> itemListEntities = new ArrayList<>();
        Elements items = s.select("ul li");
        for (int k = 0; k < items.size(); k++) {
            Element item = items.get(k);
            NovelItemListEntity listEntity = new NovelItemListEntity();
            listEntity.setBookAuthor(item.text());
            listEntity.setBookName(item.select("a").text());
            listEntity.setBookUrl(item.select("a").attr("href"));
            itemListEntities.add(listEntity);
        }
        list.setItemListEntities(itemListEntities);
        return list;
    }
}
